package xpn.platform.modules.attachment;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.data.rest.core.annotation.RepositoryRestResource;
import org.springframework.data.rest.core.config.Projection;
import xpn.platform.common.repository.XpnRepository;
import xpn.platform.modules.sys.user.SysUser;

import java.util.Date;
import java.util.List;

@RepositoryRestResource(path = "attachments", excerptProjection = AttachmentProjection.class)
public interface AttachmentDAO extends XpnRepository<Attachment, Long> {

	@Query(value = "select e from Attachment e where e.entityLink=:entityLink and e.category=:category and e.filename=:filename")
	List<Attachment> findByConditions(@Param("entityLink") String entityLink, @Param("category") String category,
			@Param("filename") String filename);

	@Query(value = "select e from Attachment e where e.entityLink=:entityLink")
	List<Attachment> findEntityAttachments(@Param("entityLink") String entityLink);

	@Query(value = "select e from Attachment e where e.entityLink in(:entityLinks) and e.category in(:categorys) order by e.createdTime desc")
	List<Attachment> queryAttachmentsInfo(@Param("entityLinks") List<String> entityLinks,@Param("categorys") List<String> categorys);

	@Query(value = "select e from Attachment e where e.entityLink in(:entityLinks)")
	List<Attachment> queryAttachmentsInfo(@Param("entityLinks") List<String> entityLinks);

	List<Attachment> findByEntityLink(String entityLink);
}

/**
 * 缺省投影，用于返回分页资源
 *
 * @author bobatkm
 *
 */
@Projection(name = "default", types = { AttachmentDAO.class })
interface AttachmentProjection {
	Long getId();

	String getEntityLink();

	String getCategory();

	String getCategoryName();

	String getFilename();

	Date getCreatedTime();

	SysUser getCreatedBy();

	@Value("#{target.fetchUrlParam()}")
	String getUrlParam();

	@Value("#{target.fetchThumbnailUrlParam()}")
	String getThumbnailUrlParam();
}
